package wang.chunfan.springcloud.learning.sentinel.server.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import wang.chunfan.springcloud.learning.sentinel.server.handler.CustomHandler;

/**
 * @version 1.0
 * @Author wangchunfan
 * @Date 2020/5/27 16:49
 * 需要在 sentinel 中配置流控规则
 **/
@RestController
@RequestMapping("rate-limit")
public class RateLimitController {

    @GetMapping("/resource")
    @SentinelResource(value = "resource", blockHandler = "handleException")
    public String resource() {
        return "按资源名称限流";
    }

    @GetMapping("/url")
    @SentinelResource(value = "url", blockHandler = "handleException")
    public String url() {
        return "按 url 限流";
    }

    @GetMapping("/custom")
    @SentinelResource(value = "custom", blockHandlerClass = CustomHandler.class)
    public String custom() {
        return "自定义限流";
    }

    public String handleException(BlockException exception) {
        return exception.getClass().getCanonicalName();
    }
}
